package com.starblink.basic.ext

import java.lang.Iterable


/**
 * java Iterable
 */
fun <E> Iterable<E>.removeWhen(predicate: Predicate<E>) {
    val it = iterator()
    while (it.hasNext()) {
        val item = it.next()
        if (predicate.invoke(item)) {
            it.remove()
        }
    }
}

/**
 * kotlin [MutableListIterator]
 */
fun <E> MutableListIterator<E>.removeWhen(predicate: Predicate<E>) {
    while (hasNext()) {
        val item = next()
        if (predicate.invoke(item)) {
            remove()
        }
    }
}

/**
 * kotlin
 */
fun <E> MutableList<E>.removeWhen(predicate: Predicate<E>) {
    listIterator().removeWhen(predicate)
}

typealias Predicate<E> = (e: E)-> Boolean